package com.kingwood.algorithm.test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * 栈想的问题
 */
public class StackRelated {
    /**
     * 找朋友
     * https://fcqian.blog.csdn.net/article/details/127342018
     * 和每日温度题类似，都是单调栈相关的问题
     * @param arr
     */
    public static void generateStudentIndexArr(int[] arr) {
        LinkedList<int[]> stack = new LinkedList<>();
        int len = arr.length;
        int[] res = new int[len];


        for (int i=0; i<len; i++) {
            int ele = arr[i];

            while (true) {
                if (stack.size() == 0) {
                    stack.add(new int[] {ele, i});
                    break;
                }

                int[] peek = stack.getLast();
                int peekEle = peek[0];
                int peekIdx = peek[1];

                if (ele > peekEle) {
                    res[peekIdx] = i;
                    stack.removeLast();
                } else {
                    stack.add(new int[] {ele, i});
                    break;
                }
            }
        }

        System.out.println(Arrays.toString(res));
    }

    public static void generateStudentIndexArr2(int[] arr) {
        int len = arr.length;
        int[] res = new int[len];
        int[] stack = new int[len];
        int r = 0;

        for (int i = 0, cur; i < len; i++) {
            while ( r > 0 && arr[i] > arr[stack[r - 1]]) {
                // 位置
                cur = stack[--r];
                res[cur] = i;
            }

            stack[r++] = i;
        }

        System.out.println(Arrays.toString(res));
    }

    public static void  generateStudentIndexArr3(int[] arr) {
        int len = arr.length;
        int[] res = new int[len];
        int[] stack = new int[len];
        int r = 0;

        for (int i = 0; i < len; i++) {
            // 弹栈
            while (r > 0 && arr[i] > arr[stack[r - 1]]) {
                int cur = stack[--r];
                res[cur] = i;
            }

            stack[r++] = i;
        }

        System.out.println(Arrays.toString(res));
    }

    static class Fractions {
        int fa; // 分母
        int ch; // 分子
        public Fractions() {
        }

        public Fractions(int fa, int ch) {
            this.fa = fa;
            this.ch = ch;
        }
    }

    static LinkedList<Fractions> numStack = new LinkedList<>();
    static LinkedList<Character> operatorStack = new LinkedList<>();

    /**
     * https://fcqian.blog.csdn.net/article/details/135005092
     * 符号运算
     * @param str
     * @return
     */
    public static String calcResult(String str) {
        String res = "";
        HashMap<Character, Integer> priority = new HashMap<>();
        priority.put('+', 1);
        priority.put('-', 1);
        priority.put('*', 2);
        priority.put('/', 2);

        StringBuilder numStr = new StringBuilder();
        int i = 0;
        while (i < str.length()) {
            char c = str.charAt(i);
            if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    numStr.append(c);
                    if (i+1 >= str.length()) {
                        break;
                    }

                    i++;
                    c = str.charAt(i);
                }

                numStack.add(new Fractions(1, Integer.parseInt(numStr.toString())));
                numStr = new StringBuilder();
            }

            if (c == '+' || c == '-' || c == '*' || c == '/') {
                while (operatorStack.size() > 0 && operatorStack.getLast() != '(' && priority.get(c) <= priority.get(operatorStack.getLast())) {
                    calc();
                }
                operatorStack.add(c);
            } else if (c == '(') {
                operatorStack.add(c);
            } else if (c == ')') {
                while (operatorStack.getLast() != '(') {
                    calc();
                }
                operatorStack.removeLast();
            }

            i++;
        }

        while (numStack.size() > 1) {
            calc();
        }

        Fractions result = numStack.getLast();

        if (result.fa == 0) {
            return "ERROR";
        }

        int k = getMaxCommonDivisor(result.fa, result.ch);
        result.fa /= k;
        result.ch /= k;

        String sign = (result.fa > 0 && result.ch < 0) || (result.fa < 0 && result.ch > 0) ? "-" : "";
        int fa = Math.abs(result.fa);
        int ch = Math.abs(result.ch);

        if (fa == 1) {
            res = sign + ch;
        } else {
            res = sign + ch + "/" + fa;
        }

        return res;
    }

    public static void calc() {
        Fractions b = numStack.removeLast();
        Fractions a = numStack.removeLast();

        char op = operatorStack.removeLast();

        Fractions res = new Fractions();
        switch (op) {
            case '+':
                res.fa = a.fa * b.fa;
                res.ch = a.ch * b.fa + b.ch * a.fa;
                break;
            case '-':
                res.fa = a.fa * b.fa;;
                res.ch = a.ch * b.fa - b.ch * a.fa;
                break;
            case '*':
                res.fa = a.fa * b.fa;
                res.ch = a.ch * b.ch;
                break;
            case '/':
                res.fa = a.fa * b.ch;
                res.ch = a.ch * b.fa;
                break;
            default:
                break;
        }

        numStack.add(res);
    }

    public static int getMaxCommonDivisor(int x, int y) {
        while (y != 0) {
            int tmp = y;
            y = x % y;
            x = tmp;
        }

        return x;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        int[] arr = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
//        generateStudentIndexArr(arr);
//        generateStudentIndexArr2(arr);
        String str = scanner.nextLine();
        System.out.println(calcResult(str));
    }
}
